/***************************************************************************
 *   Copyright (C) 2009 by mj   *
 *   mj_dv@web.de   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

// steamserverutil.cpp is a part of the steamserverutil project
// steamserverutil.googlecode.com

#include <fstream>
#include <iostream>

#include <QSettings>
#include <QTableWidgetItem>

#include "steamserverutil.h"

//to make the differance of the arrays better
#define OFF_N 0
#define OFF_C 1

using namespace std;

steamserverutil::steamserverutil()
{
    get_workspace();

    cout << "\nWorkdir: " << workdir << endl;
}

void steamserverutil::get_workspace()
{

    QSettings settings("MJ", "ssu_gui");

    workdir = settings.value("workspace", "nodir").toString().toStdString();
    
}

void steamserverutil::get_old_workspace()
{
    ifstream test_file;
    param = getenv("HOME");
    param += "/.steamserverutil/workdir";

    test_file.open(param.c_str());
    if(test_file.is_open())
    {
            getline(test_file, workdir);
    }
    else
        workdir = "nodir";
}

string steamserverutil::get_home()
{
    param.clear();
    param = getenv("HOME");
    return param;
}

void steamserverutil::set_workspace(QString str)
{

    QSettings settings("MJ", "ssu_gui");

    settings.setValue("workspace", str);

    /*ofstream test_file;
    strcpy(param, getenv("HOME"));
    strcat(param, "/.steamserverutil/workdir");

    test_file.open(param);
    if(test_file.is_open())
    {
            test_file << str;
    }
    else
        cerr << "Error while writing workdir - FATAL!\n";*/
}

void steamserverutil::system_check(int r)
{

        //0 means app exited with sucess
        //256 means screen -ls | grep &str exited without finding str, thats okay (look in screen.cpp)
        if(r == 0 || r == 256)
                return;
        else
        {
                cerr << "\nsystem_check():^error: " << r << endl;
                cerr << "Failed to execute an external dependency!\n";
        }
}

string steamserverutil::install(int choice)
{




    bool exit=false;


    //set workdir path
    param = workdir + "/installed_servers";



    ifstream in(param.c_str()); //just to look if installed_servers exits or not
    if(!in.is_open())
    {
            cout << "\n\nCreating directorys...\n";

            temp = "mkdir " + workdir + "/servers_source && mkdir " + workdir + "/servers_hl1";


            system_check(system(param.c_str()));
    }
    in.close();


    ofstream out;
    out.open(param.c_str(), ios_base::app);



    QComboBox *dummy = new QComboBox(); //only for pretending a comboBox for is_installed(QComboBox)

    is_installed(dummy);



    string _install;

    _install = workdir + "/";





    switch(choice)
    {
            case 1: 	cout << "\n\nInstalling Counter Strike-Source Server...\nAdding to installed_server file\n\n";

            if(!server_css)
                    out << "\ncss";
            _install += "./steam -command update -game \"Counter-Strike Source\" -dir " + workdir + "/servers_source/";

            system_check(system(_install.c_str()));
            break;


            case 2: 	cout << "\n\nInstalling Day of Defeat Source Server...\nAdding to installed_server file\n\n";
            if(!server_dods)
                    out << "\ndods";
            _install += "./steam -command update -game \"dods\" -dir " + workdir + "/servers_source/";


            system_check(system(_install.c_str()));
            break;


            case 3: 	cout << "\n\nInstalling Garrys Mod Server...\nAdding to installed_server file\n\n";
            if(!server_garrysmod)
                    out << "\ngarrysmod";
            _install += "./steam -command update -game \"garrysmod\" -dir " + workdir + "/servers_source/";

            system_check(system(_install.c_str()));
            break;


            case 4: 	cout << "\n\nInstalling Half Life 2 Deathmatch Server...\nAdding to installed_server file\n\n";
            if(!server_hl2mp)
                    out << "\nhl2mp";
            _install += "./steam -command update -game \"hl2mp\" -dir " + workdir + "/servers_source/";

            system_check(system(_install.c_str()));
            break;



            case 5: 	cout << "\n\nInstalling Team Fortress Server...\nAdding to installed_server file\n\n";
            if(!server_tf)
                    out << "\ntf";
            _install += "./steam -command update -game \"tf\" -dir " + workdir + "/servers_source/";


            system_check(system(_install.c_str()));
            break;


            case 6: 	cout << "\n\nInstalling Counter Strike Server...\nAdding to installed_server file\n\n";
            if(!server_cs)
                    out << "\ncstrike";
            _install += "./steam -command update -game \"cstrike\" -dir " + workdir + "/servers_hl1/";
            system_check(system(_install.c_str()));
            break;


            case 7: 	cout << "\n\nInstalling Counter Strike Condition Zero Server...\nAdding to installed_server file\n\n";
            if(!server_czero)
                    out << "\ncscz";
            _install += "./steam -command update -game \"czero\" -dir " + workdir + "/servers_hl1/";


            system_check(system(_install.c_str()));
            break;


            case 8: 	cout << "\n\nInstalling Death Match Classic Server...\nAdding to installed_server file\n\n";
            if(!server_dmc)
                    out << "\ndmc";
            _install += "./steam -command update -game \"dmc\" -dir " + workdir + "/servers_hl1/";

            system_check(system(_install.c_str()));
            break;


            case 9: 	cout << "\n\nInstalling Day of Defeat Server...\nAdding to installed_server file\n\n";
            if(!server_dod)
                    out << "\ndod";
            _install += "./steam -command update -game \"dod\" -dir " + workdir + "/servers_hl1/";


            system_check(system(_install.c_str()));
            break;


            case 10: 	cout << "\n\nInstalling Opposing Force Server...\nAdding to installed_server file\n\n";
            if(!server_gearbox)
                    out << "\ngearbox";
            _install += "./steam -command update -game \"gearbox\" -dir " + workdir + "/servers_hl1/";

            system_check(system(_install.c_str()));
            break;


            case 11: 	cout << "\n\nInstalling Ricochet Server...\nAdding to installed_server file\n\n";

            if(!server_ricochet)
                    out << "\nricochet";
            _install += "./steam -command update -game \"ricochet\" -dir " + workdir + "/servers_hl1/";

            system_check(system(_install.c_str()));
            break;


            case 12: 	cout << "\n\nInstalling Team fortress Classic Server...\nAdding to installed_server file\n\n";
            if(!server_tfc)
                    out << "\ntfc";
            _install += "./steam -command update -game \"tfc\" -dir " + workdir + "/servers_hl1/";


            system_check(system(_install.c_str()));
            break;


            case 13: 	cout << "\n\nInstalling Dark Messiah Server...\nCreating directory...\nAdding to installed_server file\n\n";


            if(!out.is_open())
            {

                    temp = "mkdir " + workdir + "/server_darkmessiah/";
                    system_check(system(temp.c_str()));
            }

            if(!server_darkmessiah)
                    out << "\ndarkmessiah";
            _install += "./steam -command update -game \"darkmessiah\" -dir " + workdir + "/server_darkmessiah/";

            system_check(system(_install.c_str()));
            break;


            case 14: 	cout << "\n\nInstalling Age of Chivalry Server...\n\nAdding to installed_server file\n\n";
            if(!server_chivalry)
                    out << "\nchivalry";
            _install += "./steam -command update -game \"ageofchivalry\" -dir " + workdir + "/servers_source/";

            system_check(system(_install.c_str()));
            break;


            case 15: 	cout << "\n\nInstalling D.I.P.R.I.P. Warm Up (HL2 Mod) Server...\nAdding to installed_server file\n\n";
            if(!server_diprip)
                    out << "\ndiprip";
            _install += "./steam -command update -game \"diprip\" -dir " + workdir + "/servers_source/";

            system_check(system(_install.c_str()));
            break;


            case 16: 	cout << "\n\nInstalling Insurgency (HL2 Mod) Server...\nAdding to installed_server file\n\n";
            if(!server_insurgency)
                    out << "\ninsurgency";
            _install += "./steam -command update -game \"insurgency\" -dir " + workdir + "/servers_source/";
            system_check(system(_install.c_str()));
            break;


            case 17: 	cout << "\n\nInstalling Synergy (HL2 Mod) Server...\nAdding to installed_server file\n\n";
            if(!server_synergy)
                    out << "\nsynergy";
            _install += "./steam -command update -game \"synergy\" -dir " + workdir +  "/servers_source/";

            system_check(system(_install.c_str()));
            break;


            case 18: 	cout << "\n\nInstalling Zombie Panic: Source (HL2 Mod) Server...\nAdding to installed_server file\n\n";
            if(!server_zps)
                    out << "\nzps";
            _install += "./steam -command update -game \"zps\" -dir " + workdir + "/servers_source/";

            system_check(system(_install.c_str()));
            break;

            case 19:cout << "\n\nInstalling Counter Eternal Silence (mod)...\nAdding to installed_server file\n\n";

            if(!server_esmod)
                    out << "\nesmod";
            _install += "./steam -command update -game \"esmod\" -dir " + workdir + "/servers_source/";
            system_check(system(_install.c_str()));

            break;

            case 20:
                    cout << "\n\nInstalling Left 4 Dead...\nAdding to installed_server file\n\n";

                    if(!server_l4d)
                            out << "\nl4d";
                    _install += "./steam -command update -game \"l4d_full\" -dir " + workdir + "/servers_source/";
                    system_check(system(_install.c_str()));
                    break;

            case 21: 	exit=true;
            break;


            default: 	cout << "\n\nTry again;)\n\n";
            break;
    }

    out.close();
    return _install;



}

void steamserverutil::install_mani(int index)
{

    server_path(server[index]);

    //writes only vdf file
    ofstream save_vdf;

    cout << "\n\nCreating mani_admin_plugin.vdf\n";

    param += "/addons/mani_admin_plugin.vdf";

    save_vdf.open(param.c_str()); //right path

    //with choice right entry
    switch (server[index])
    {

            case 1:


                    save_vdf << "\"Plugin\"\n";
                    save_vdf << "{\n";
                    save_vdf << " \"file\" \"../cstrike/addons/mani_admin_plugin\"\n";
                    save_vdf << "}";
                    break;

            case 2:


                    save_vdf << "\"Plugin\"\n";
                    save_vdf << "{\n";
                    save_vdf << " \"file\" \"../dod/addons/mani_admin_plugin\"\n";
                    save_vdf << "}";
                    break;

            case 3:

                    save_vdf << "\"Plugin\"\n";
                    save_vdf << "{\n";
                    save_vdf << " \"file\" \"../garrysmod/addons/mani_admin_plugin\"\n";
                    save_vdf << "}";

                    save_vdf.close();

                    //param[strlen(param.c_str()) - 29] = /*to delete old path, see above*/
                    param.erase(param.size() - 29, param.size());
                    param += "/cfg/server.cfg";

                    save_vdf.open(param.c_str(), ios_base::app);

                    save_vdf << "\n\nexec mani_server\n"; //needed otherwise gmod can not handle mani admin plugin
                    break;


            case 4:


                    save_vdf << "\"Plugin\"\n";
                    save_vdf << "{\n";
                    save_vdf << " \"file\" \"../hl2mp/addons/mani_admin_plugin\"\n";
                    save_vdf << "}";
                    break;

            case 5:

                    save_vdf << "\"Plugin\"\n";
                    save_vdf << "{\n";
                    save_vdf << " \"file\" \"../tf/addons/mani_admin_plugin\"\n";
                    save_vdf << "}";
                    break;



              default: cout << "Error installing mani! - FATAL!";
                    break;

    }

    save_vdf.close();


    cout << "\n\nMani Admin Plugin succesfully installed!\n";


}


bool steamserverutil::is_installed(QComboBox *comboBox)
{
    ifstream in;

    //set workdir path
    param = workdir + "/installed_servers";

    in.open(param.c_str());

    if(!in.is_open())
    {
        return false;
    }

    bool installed = false;


    i = 0;


    //testing if games are installed



    while (!in.eof())
    {
            char temp[50];
            in.getline (temp, 50);

            if(!strncmp(temp, "css", 3))

            {

                    comboBox->addItem("Counter Strike Source");
                    server[i] = 1;
                    server_css = true;
                    i++;
                    installed = true;


            }

            if(!strncmp(temp, "dods", 4))

            {
                    comboBox->addItem("Day of Defeat Source");
                    server[i] = 2;
                    server_dods=true;
                    i++;
                    installed = true;

            }


            if(!strncmp(temp, "garrysmod", 9))
            {
                    comboBox->addItem("Garrys Mod");
                    server[i] = 3;
                    server_garrysmod = true;
                    i++;
                    installed = true;

            }


            if(!strncmp(temp, "hl2mp", 5))
            {
                    comboBox->addItem("Half Life 2 Deathmatch");
                    server[i] = 4;
                    server_hl2mp=true;
                    i++;
                    installed = true;

            }


            if(!strncmp(temp, "tf", 2))
            {
                    comboBox->addItem("Team Fortress");
                    server[i] = 5;
                    server_tf = true;
                    i++;
                    installed = true;

            }


            if(!strncmp(temp, "cstrike", 7))
            {
                    comboBox->addItem("Counter Strike");
                    server[i] = 6;
                    server_cs=true;
                    i++;
                    installed = true;

            }

            if(!strncmp(temp, "cscz", 4))
            {
                    comboBox->addItem("Counter Strike Condition Zero");
                    server[i] = 7;
                    server_czero = true;
                    i++;
                    installed = true;

            }


            if(!strncmp(temp, "dmc", 3))
            {
                    comboBox->addItem("Deathmatch Classic");
                    server[i] = 8;
                    server_dmc=true;
                    i++;
                    installed = true;

            }


            if(!strncmp(temp, "dods", 4))
            {
                    if(!strncmp(temp, "dod", 3))

                    {
                            comboBox->addItem("Day of Defeat");
                            server[i] = 9;
                            server_dod = true;
                            i++;
                            installed = true;

                    }
            }


            if(!strncmp(temp, "gearbox", 7))
            {
                    comboBox->addItem("Opposing Force");
                    server[i] = 10;
                    server_gearbox=true;
                    i++;
                    installed = true;

            }


            if(!strncmp(temp, "ricochet", 8))
            {
                    comboBox->addItem("Ricochet");
                    server[i] = 11;
                    server_ricochet = true;
                    i++;
                    installed = true;

            }


            if(!strncmp(temp, "tfc", 3))
            {
                    comboBox->addItem("Team Fortress Classic");
                    server[i] = 12;
                    server_tfc=true;
                    i++;
                    installed = true;

            }


            if(!strncmp(temp, "darkmessiah", 11))
            {
                    comboBox->addItem("Dark Messiah");
                    server[i] = 13;
                    server_darkmessiah=true;
                    i++;
                    installed = true;

            }

            if(!strncmp(temp, "chivalry", 8))
            {
                    comboBox->addItem("Age of Chivalry");
                    server[i] = 14;
                    server_chivalry=true;
                    i++;
                    installed = true;

            }

            if(!strncmp(temp, "diprip", 6))
            {
                    comboBox->addItem("D.I.P.R.I.P. Warm Up (HL2 Mod)");
                    server[i] = 15;
                    server_diprip=true;
                    i++;
                    installed = true;

            }

            if(!strncmp(temp, "insurgency", 10))
            {
                    comboBox->addItem("Insurgency (HL2 Mod)");
                    server[i] = 16;
                    server_insurgency=true;
                    i++;
                    installed = true;

            }

            if(!strncmp(temp, "synergy", 7))
            {
                    comboBox->addItem("17. Synergy (HL2 Mod)");
                    server[i] = 17;
                    server_synergy=true;
                    i++;
                    installed = true;

            }

            if(!strncmp(temp, "zps", 3))
            {
                    comboBox->addItem("Zombie Panic: Source (HL2 Mod)");
                    server[i] = 18;
                    server_zps=true;
                    i++;
                    installed = true;

            }

            if(!strncmp(temp, "esmod", 5))
            {
                    comboBox->addItem("Enternal Silence (Mod)");
                    server[i] = 19;
                    server_esmod=true;
                    i++;
                    installed = true;

            }

            if(!strncmp(temp, "l4d", 3))
            {
                    comboBox->addItem("Left 4 Dead");
                    server[i] = 20;
                    server_l4d=true;
                    i++;
                    installed = true;

            }



    }
    in.close();

    i = 0;


    return installed;
}

void steamserverutil::start_server(int index)
{

    param =  workdir + "/";

    switch(server[index])
    {
            case 1:
            {





                           param += "/servers_source/srcds_run -game cstrike ";



            }

            break;
            case 2:
            {


                           param += "/servers_source/srcds_run -game dod ";




            }

            break;

            case 3:
            {

                            param += "/servers_source/srcds_run -game garrysmod ";




            }

            break;
            case 4:

                            param += "/servers_source/srcds_run -game hl2mp ";



            break;
            case 5:


                            param += "/servers_source/srcds_run -game tf";



            break;
            case 6:
                           param += "/servers_hl1/hlds_run -game cstrike ";


            break;
            case 7:
                            param += "/servers_hl1/hlds_run -game czero ";



            break;
            case 8:
                            param += "/servers_hl1/hlds_run -game dmc ";




            break;
            case 9:
                            param += "/servers_hl1/hlds_run -game dod";



            break;
            case 10:
                            param += "/servers_hl1/hlds_run -game gearbox";



            break;
            case 11:
                           param += "/servers_hl1/hlds_run -game ricochet";

            break;
            case 12:
                            param += "/servers_hl1/hlds_run -game tfc";

            break;
            case 13:
                            param += "/server_darkmessiah/srcds_run ";



            break;

            case 14:
                            param += "/servers_source/srcds_run -agme ageofchivalry ";


            break;

            case 15:
                            param += "/servers_source/srcds_run -game diprip ";

            break;
            case 16:
                           param += "/servers_source/srcds_run -game insurgency ";

            break;
            case 17:
                            param += "/servers_source/srcds_run -game synergy ";

            break;
            case 18:
                            param += "/servers_source/srcds_run -game zps ";

            break;
            case 19:
                            param += "/servers_source/srcds_run -game esmod ";

            break;
            case 20:
                            param += "/servers_source/srcds_run -game l4d ";

            break;
            default: cerr << "Error with server[index] in switch start_server";
            break;
    }

    cout << "\nParam: " << param << endl;
}

void steamserverutil::server_path(unsigned short int choice)
{

    param = workdir + "/"; //using global string here

    switch(choice)
    {
            case 1: //if(server_css==true)
            {

                    param += "servers_source/cstrike/";

            }


            break;
            case 2: //if(server_dods==true)
            {
                    param += "servers_source/dod/";



            }


            break;
            case 3: //if(server_garrysmod==true)
            {
                    param += "servers_source/garrysmod/";



            }


            break;
            case 4: //if(server_hl2mp==true)
            {
                   param += "servers_source/hl2mp/";



            }


            break;
            case 5: //if(server_tf==true)
            {
                    param += "servers_source/tf/";


            }


            break;
            case 6: //if(server_cs==true)
            {
                    param += "servers_hl1/cstrike/";



            }


            break;
            case 7: //if(server_czero==true)
            {
                    param += "servers_hl1/czero/";



            }


            break;
            case 8: //if(server_dmc==true)
            {
                    param += "servers_hl1/dmc/";



            }


            break;
            case 9: //if(server_dod==true)
            {
                    param += "servers_hl1/dod/";



            }


            break;
            case 10:        //if(server_gearbox==true)
            {
                   param += "servers_hl1/gearbox/";



            }


            break;
            case 11:        //if(server_ricochet==true)
            {
                    param += "servers_hl1/ricochet/";



            }


            break;
            case 12:        //if(server_tfc==true)
            {
                    param += "servers_hl1/tfc/";



            }


            break;
            case 13:        //if(server_darkmessiah==true)
            {

                    param += "server_darkmessiah/";



            }


            break;
            case 14:        //if(server_chivalry==true)
            {

                   param += "servers_source/ageofchivalry/";



            }


            break;
            case 15:        //if(server_diprip==true)
            {

                    param += "servers_source/diprip/";



            }


            break;
            case 16:        //if(server_insurgency==true)
            {

                    param += "servers_source/insurgency/";



            }


            case 17:        //if(server_synergy==true)
            {

                    param += "servers_source/sunergy/";



            }


            break;
            case 18:        //if(server_zps==true)
            {

                    param += "servers_source/zps/";


            }


            break;
            case 19:        //if(server_esmod==true)
            {

                   param += "servers_source/esmod/";


            }


            break;
            case 20:        //if(server_l4d==true)
            {


                   param += "servers_source/l4d/";


            }


            break;
            default: cout << "\n\nTry again ;)\n\n";
            break;
    }

    cout << "\nServer_path(): " << param;
}


void steamserverutil::edit_mapcl(int x, int index, int choice, QTableWidget *mapTable)
{
        //x=1 to edit maplist.txt
        //x=2 to edit mapcycle.txt
        //x=3 to edit votemaplist.txt (only with mani ad
        server_path(server[index]);







        string mapfolder;

        mapfolder = param + "/maps/";

        if (x==1)
        {
                param += "/maplist.txt";

        }
        if(x==2)
        {
                param += "/mapcycle.txt";

        }
        if(x==3)
        {
                param += "/cfg/mani_admin_plugin/votemaplist.txt";
        }

        cout << "Param: "<< param << endl;




        bool exit =false;

        while(!exit)
        {

                if((x !=1) && (x !=2) && x !=3)
                {
                        //never_happen();
                        break;
                }


                if (x==1)
                {
                        cout << "\n\nHere is your current maplist: \n\n";
                }
                if(x==2)
                {

                        cout << "\n\nHere is your current mapcycle: \n\n";
                }
                if(x==3)
                {
                        cout << "\n\nHere is your current votemaplist: \n\n";
                }




                //x=1 to edit maplist.txt
                //x=2 to edit mapcycle.txt
                //x=3 to edit votemaplist.txt (only with mani admin plugin)


                string temp;

                switch(choice)
                {
                        case 1: //writes now all data from mapTable



                        {
                                ofstream save_del;
                                save_del.open(param.c_str());

                                for (unsigned short int y = 0; y< mapTable->rowCount(); y++)
                                {


                                        save_del << mapTable->item(y, 0)->text().toStdString() << endl;

                                }
                                save_del.close();
                        }
                        exit = true;
                        break;

                        case 3: {
                                string map;

                                map[0]='^'; // to avoid errors with strncmp()

                                temp = "ls -1 " + mapfolder + " > temp.txt";

                                system_check(system(temp.c_str()));



                                        ifstream in_temp;
                                        in_temp.open("temp.txt");

                                        ofstream out;
                                        out.open(param.c_str());

                                        if(in_temp.is_open())
                                        {
                                                while(!in_temp.eof())
                                                {

                                                        getline(in_temp, temp);


                                                        if(!temp.compare(0, map.size()-1, map.c_str()))
                                                        {
                                                                continue;
                                                        }

                                                        if(!temp.compare("materials"))
                                                        {
                                                                continue;
                                                        }

                                                        if(!temp.compare("graphs"))
                                                        {
                                                                continue;
                                                        }

                                                        if(!temp.compare("soundcache"))
                                                        {
                                                                continue;
                                                        }


                                                        map.clear();

                                                        for(unsigned int i=0; i <= temp.size(); i++)
                                                        {
                                                                int k = 0;
                                                                switch(temp[i])
                                                                {
                                                                        case '.': k=1;
                                                                        break;
                                                                        default: map += temp[i];
                                                                        break;
                                                                }
                                                                if(k==1)
                                                                {
                                                                        map += '\0'; //0 means end of string
                                                                        break;
                                                                }
                                                        }
                                                        out << map << endl;
                                                }
                                        }
                                        else
                                        {
                                                cout << "Cannot open temp.txt";
                                        }

                                        in_temp.close();
                                        out.close();
                                }



                                system_check(system("rm -rf temp.txt"));
                                exit = true;

                                break;


         case 2:
                {
                   ifstream input; //to read current *.txt
                input.open(param.c_str());

                vector<string> maps;



                int i;

                i=0;

                if(input.is_open())
                {
                        while(!input.eof())
                        {


                                getline(input, temp);

                                if(temp[0] == 10 || temp[0] == 0) //10 means new line, 0 means end of string
                                {
                                        //nothing to do
                                }
                                else
                                {
                                        maps.push_back(temp);
                                        cout << (i+1) << ".\t" << maps[i] << endl;
                                        mapTable->insertRow(i);
                                        mapTable->setItem(i, 0, new QTableWidgetItem(maps[i].c_str()));
                                        i++;
                                }

                        }
                }
                else
                {
                        cout << "\nNo maps. Please add one\n";

                }
                input.close();
                exit=true;

                                break;
                }

                default: cout << "\n\nWrong choice. Try again ;)";
                                break;
                }






        }

}



void steamserverutil::servercfg(int index, int choice, QTableWidget *servercfgTable)
{


        server_path(server[index]);

        param += "cfg/server.cfg";


        bool exit = false;

        while(!exit)
        {



                switch(choice)
                {



                        break;
                        case 1:


                        {

                                ofstream save_del;
                                save_del.open(param.c_str());




                                int rows = servercfgTable->rowCount();

                                for (int i = 0;i<rows;i++)
                                {

                                        save_del << "//n: " << servercfgTable->item(i, 0)->text().toStdString() << endl;
                                        save_del << servercfgTable->item(i, 1)->text().toStdString() << endl;


                                }
                                save_del.close();
                        }

                        exit = true;
                        break;


                        case 2:
                        {
                        ifstream in;
                    in.open(param.c_str());

                    vector<string> input[2];
                    int counter = 0;

                    cout << "\n\nHere is your current server.cfg: \n\n\n";

                    if(in.is_open())


                    {

                        while(!in.eof())
                        {

                                getline(in, temp);

                                switch(temp[0])
                                {
                                        case '/': if(!temp.compare(0, 5, "//n: "))
                                        {
                                                input[OFF_N].push_back(&temp[5]);
                                                cout << (counter+1) << ".\t" << input[OFF_N].at(counter) << endl;
                                                servercfgTable->insertRow(counter);
                                                servercfgTable->setItem(counter, 0, new QTableWidgetItem(input[OFF_N][counter].c_str()));
                                                //counter++;
                                        }
                                        break;
                                        case 0: break; //end of string
                                        case 10: break; //new line
                                        default: input[OFF_C].push_back(temp);
                                                 servercfgTable->setItem(counter, 1, new QTableWidgetItem(input[OFF_C][counter].c_str()));
                                                  counter++;

                                        break;

                                }

                        }


                    }
                    else
                            cout << "\n\nNo commands in server.cfg, please add one ;)\n\n";

                    in.close();

                    exit=true;
                        break;

                }

                        default: cout << "\n\nTry again ;)\n";
                }

        }




}


void steamserverutil::mani_admins(int index, int choice, QTableWidget *adminTable)
{
        bool exit = false;




        server_path(server[index]);

        string params;


        temp = "cd " + param + " && " + "cp cfg/mani_admin_plugin/adminlist.txt.old cfg/mani_admin_plugin/adminlist.txt";
        system_check(system(temp.c_str()));


        param += "/cfg/mani_admin_plugin/adminlist.txt";


        ifstream in;
        in.open(param.c_str());

        cout << "param: " << param << endl;
        cout << "temp: " << temp << endl;

        cout << endl << endl;

        while (!exit)
        {


                switch(choice)
                {


                          case 1:
                                {
                        ofstream save_del;
                         save_del.open(param.c_str());
                                        int rows = adminTable->rowCount();

                                        for (int i = 0;i<rows;i++)
                                        {


                                            string temp = adminTable->item(i, 1)->text().toStdString();

                                            if(temp[0] == 'S')
                                                save_del << temp << endl;
                                            else
                                                save_del << ";" << temp << endl;


                                            save_del << "//n: " << adminTable->item(i, 0)->text().toStdString() << endl;



                                        }
                                        save_del.close();
                                }
                    exit = true;
                                break;
                                case 2:
                                   {
                                    vector<string> input[2]; //OFF_N = 0 means name; OFF_C = 1 means steam id or ip




                                    int counter = 0;

                                    if(in.is_open())
                                    {

                                        cout << "\n\nCurrent admins: \n";

                                        while(!in.eof())
                                        {
                                                getline(in, temp);




                                                        switch(temp[0])
                                                        {
                                                               case 'S': if(!temp.compare(0, 5, "STEAM"))
                                                                {
                                                                        input[OFF_C].push_back(temp);
                                                                        cout << "Steam ID: " <<  input[OFF_C][counter] << endl;
                                                                        adminTable->insertRow(counter);

                                                                        adminTable->setItem(counter, 1, new QTableWidgetItem(input[OFF_C][counter].c_str()));
                                                                }
                                                                else
                                                                        cout << "\n\nCannot understand: " << temp << "\nIgnoring...";

                                                                break;

                                                                case ';': input[OFF_C].push_back(temp);
                                                                          cout << "Ip: " <<  input[OFF_C][counter] << endl;
                                                                          adminTable->insertRow(counter);
                                                                        adminTable->setItem(counter, 1, new QTableWidgetItem(&input[OFF_C][counter][1]));
                                                                break;
                                                                case '/': if(!temp.compare(0, 5, "//n: "))
                                                                {
                                                                        input[OFF_N].push_back(&temp[5]);
                                                                        cout << (counter+1) << ".\t" << input[OFF_N][counter] << endl;
                                                                        adminTable->setItem(counter, 0, new QTableWidgetItem(input[OFF_N][counter].c_str()));
                                                                        counter++;
                                                                }
                                                                break;
                                                                default:
                                                                        if(temp[0] == 10 || temp[0]== 0) //10 means new line 0 means end of string
                                                                        {
                                                                                //nothing to do
                                                                        }
                                                                        else
                                                                                cout << "\n\nCannot understand: " << temp << "\nIgnoring...";
                                                        }
                                                }
                                        }
                                        else
                                        {
                                                cout << "\nNo admins. Please add one\n";

                                        }
                                        in.close();
                                    }
                                    exit=true;

                                break;
                                default: cout << "\n\nTry again;)\n\n";
                                break;
                }



        }

}

void steamserverutil::wordfilter(int index, int choice, QTableWidget *filterTable)
{
        bool exit=false;





        server_path(server[index]);





        param += "/cfg/mani_admin_plugin/wordfilter.txt";

        vector<string> filter;



        while(!exit)
        {





                switch (choice)
                {

                        case 1:
                        {


                                ofstream save_del;
                                save_del.open(param.c_str());



                                int rows = filterTable->rowCount();

                                for (int i = 0;i<rows;i++)
                                {


                                    save_del << filterTable->item(i, 0)->text().toStdString() << endl;

                                }
                                save_del.close();
                        }
                    exit = true;
                        break;

                        case 2:
                        {
                            int counter = 0;


                            ifstream in;
                            in.open(param.c_str());

                            cout << "\nhere is your wordfilter:  \n";

                            if(in.is_open())
                            {
                                    while (!in.eof())
                                    {

                                            getline(in, temp);

                                            switch(temp[0])
                                            {
                                                    case '/': //nothing to do
                                                            break;
                                                    case 10: //nothing to do (10 means new line)
                                                            break;
                                                    case 0: //nothing to do (0 means end of string)
                                                            break;
                                                    default: filter.push_back(temp);
                                                            cout << (counter + 1) << "\t" << filter[counter] << endl;
                                                            filterTable->insertRow(index);
                                                            filterTable->setItem(counter, 0, new QTableWidgetItem(filter[counter].c_str()));
                                                            counter++;
                                                            break;
                                            }
                                    }
                            }
                            else
                            cout << "\nNo wordfilters. Please add one!\n";
                            exit=true;
                             break;
                         }
                        default: cout << "\n\nTry again;)\n\n";
                        break;
                }


    }

}

void steamserverutil::rcon(int index, int choice, QTableWidget *rconTable)
{



        bool exit = false;

        server_path(server[index]);


        vector<string> buf[2]; //OFF_name means 0, OFF_C means 1







        param += "/cfg/mani_admin_plugin/rconlist.txt";






            while (!exit)
            {






                    switch(choice)
                    {


                            case 1:
                            {

                                    ofstream save_del;
                                    save_del.open(param.c_str());



                                    int rows = rconTable->rowCount();

                                    for (int i = 0;i<rows;i++)
                                    {


                                         save_del << "\"" << rconTable->item(i, 0)->text().toStdString() << "\"" << " " << rconTable->item(i, 1)->text().toStdString() << endl;


                                    }
                                    save_del.close();
                            }
                        exit = true;
                            break;

                            case 2:

                                {

                                    unsigned short int counter = 0;

                    ifstream in;
                    in.open(param.c_str());

                    if(in.is_open())
                    {


                            cout << "\n\nCurrent rconlist: \n";


                            while(!in.eof())
                            {
                                    getline(in, temp);

                                    switch(temp[0])
                                    {
                                            case '"': unsigned int i;
                                            //force a new string in the vector
                                            buf[OFF_N].push_back("");
                                            buf[OFF_C].push_back("");

                                            for(i=1; i<temp.size(); i++)
                                            {
                                                    if(temp[i]=='"')
                                                            break;

                                                    buf[OFF_N][counter] += temp[i];
                                            }

                                             buf[OFF_N][counter] += '\0'; //0 means end of string

                                            cout << (counter+1) << ".\t" << buf[OFF_N][counter] << endl;

                                            rconTable->insertRow(counter);
                                            rconTable->setItem(counter, 0, new QTableWidgetItem(buf[OFF_N][counter].c_str()));



                                            i+=2; //to junp over the '"' and over the ' '

                                            for(; i<temp.size(); i++)
                                            {
                                                    if((temp[i]=='/' && temp[(i+1)] == '/') || temp[i] == 0) //0 means end of string
                                                            break;

                                                    buf[OFF_C][counter] += temp[i];
                                            }
                                             buf[OFF_C][counter] += '\0'; //0 means end of string

                                            rconTable->setItem(counter, 1, new QTableWidgetItem(buf[OFF_C][counter].c_str()));

                                            counter++;

                                            temp.clear(); //clearing, to avoid the bug with the letter when temp[] = "\"\" "
                                            break;

                                            case 10: //10 means new line
                                            //nothing to do
                                            break;
                                            case 0://0 means end of string
                                            //nothing to do
                                                    break;
                                            case '/': if (temp [1] == '/')
                                                    break;

                                            default: buf[OFF_N].push_back(temp);
                                                        buf[OFF_C].push_back(temp);

                                            cout << (counter+1) << ".\t" << buf[OFF_N][counter] << endl;
                                            rconTable->setItem(counter, 0, new QTableWidgetItem(buf[OFF_N][counter].c_str()));
                                            rconTable->setItem(counter, 1, new QTableWidgetItem(buf[OFF_C][counter].c_str()));

                                            counter++;

                                    }



                            }

                            in.close();

                    }

                    else
                            cout << "\nYou have no rcon commands, please add one!\n";

                    exit = true;
                            break;
                        }
                            default: cout << "\n\nTry again;)\n";
                    }
            }

}


void steamserverutil::votequestionlist(int index, int choice, QTableWidget *voteTable)
{

        bool exit = false;

        server_path(server[index]);


        vector<string> buf[2]; //OFF_name means 0, OFF_C means 1






        param += "/cfg/mani_admin_plugin/votequestionlist.txt";






                while (!exit)
                {


                        switch(choice)
                        {



                                case 1:
                                {

                                        ofstream save_del;
                                        save_del.open(param.c_str());


                                        int rows = voteTable->rowCount();


                                        for (int i = 0;i<rows;i++)
                                        {

                                            save_del << "\"" << voteTable->item(i, 0)->text().toStdString() << "\"" << " \"" << voteTable->item(i, 1)->text().toStdString() << "\"" << endl;


                                        }
                                        save_del.close();
                                }
                                exit=true;
                                break;

                                case 2:
                                {
                                    unsigned short int counter = 0;

                        ifstream in;
                        in.open(param.c_str());

                        if(in.is_open())
                        {
                                cout << "\n\nCurrent votequestionlist: \n";


                                while(!in.eof())
                                {
                                        getline(in, temp);

                                        unsigned int i = 0;
                                        switch(temp[0])
                                        {
                                                //force a new string in the vector
                                                buf[OFF_N].push_back("");
                                                buf[OFF_C].push_back("");

                                                for(i=1; i<temp.size(); i++)
                                                {
                                                        if(temp[i]=='"')
                                                                break;

                                                        buf[OFF_N][counter] += temp[i];
                                                }

                                                buf[OFF_N][counter] += '\0'; //0 means end of string

                                                cout << (counter+1) << ".\t" << buf[OFF_N][counter] << endl;

                                                voteTable->insertRow(counter);
                                                voteTable->setItem(counter, 0, new QTableWidgetItem(buf[OFF_N][counter].c_str()));



                                                i+=3; //to junp over the '"' and over the ' ' and the second '"'


                                                for(; i<temp.size(); i++)
                                                {
                                                        if(temp[i]=='"')
                                                                break;

                                                        buf[OFF_C][counter] += temp[i];
                                                }


                                                buf[OFF_C][counter] += '\0'; //0 means end of string

                                                         // cout << buf[0][OFF_C] << " "  << buf[1][OFF_C];
                                                voteTable->setItem(counter, 1, new QTableWidgetItem(buf[OFF_C][counter].c_str()));

                                                counter++;

                                                temp.clear(); //clearing to avoid the bug with the letter when temp[] = "\"\" "

                                                break;



                                        }

                                }

                                in.close();
                        }

                        else
                                cout << "\nYou have no votequestions, please add one!\n";

                                exit = true;
                                break;
                            }
                                default: cout << "\n\nTry again;)\n";
                                break;

                        } //switch

                }//while(!exit)





}

void steamserverutil::ping_reserve(int x, int index, int choice, QTableWidget *reserveTable)
{
        //x = 1 means reserveslots, x = 2 means pingimmunity

       server_path(server[index]);

        if(x==1)
              param += "/cfg/mani_admin_plugin/reserveslots.txt";
        else
              param += "/cfg/mani_admin_plugin/pingimmunity.txt";


            switch(choice)
            {

                       case 1:
                            {

                                    ofstream save_del;
                                    save_del.open(param.c_str());



                                    int rows = reserveTable->rowCount();

                                    for (int i = 0;i<rows;i++)
                                    {

                                        string temp = reserveTable->item(i, 1)->text().toStdString();

                                            if(temp[0] == 'S')
                                                save_del << temp << endl;
                                            else
                                                save_del << ";" << temp << endl;



                                            save_del << "//n: " << reserveTable->item(i, 0)->text().toStdString() << endl;


                                    }
                                    save_del.close();
                            }
                            break;

                       case 2:
                            {


                cout << endl << endl;




                       vector<string> input[2]; //OFF_N = 0 means name; OFF_C = 1 means steam id or ip

                        ifstream in;
                        in.open(param.c_str());


                        int counter = 0;

                        if(in.is_open())
                        {

                                cout << "\n\nCurrent ";

                                if(x==1)
                                        cout << "reserveslots: \n";
                                else
                                        cout << "pingimmunities: \n";

                                while(!in.eof())
                                {
                                       getline(in, temp);



                                        switch(temp[0])
                                        {
                                                case 'S': if(!temp.compare(0, 5, "STEAM"))
                                                {
                                                        input[OFF_C].push_back(temp);
                                                        reserveTable->insertRow(counter);
                                                        reserveTable->setItem(counter, 1, new QTableWidgetItem(input[OFF_C][counter].c_str()));
                                                }
                                                else
                                                        cout << "\n\nCannot understand: " << temp << "\nIgnoring...\n";

                                                break;

                                                case '/': if(!temp.compare(0, 5, "//n: "))
                                                {
                                                        input[OFF_N].push_back(&temp[5]);
                                                        cout << (counter+1) << ".\t" << input[OFF_N][counter] << endl;

                                                        reserveTable->setItem(counter, 0, new QTableWidgetItem(input[OFF_N][counter].c_str()));
                                                        counter++;
                                                }
                                                break;
                                                default:
                                                        if(temp[0] == 10 || temp[0]== 0 || temp[0] == 13) //10 means new line 0 means end of string 13 means "\r"
                                                        {
                                                                //nothing to do
                                                        }
                                                        else
                                                                cout << "\n\nCannot understand: " << temp << "\nIgnoring...\n";

                                                        break;
                                        }


                        }
                            }
                        else
                        {
                                cout << "\nNo entrys. Please add one\n";

                        }
                        in.close();
                            break;
                        }
                            default: cout << "\n\nTry again;)\n\n";
                            break;
            }




}


void steamserverutil::voterconlist(int index, int choice, QTableWidget *voteRconTable)
{



       vector<string> buf[3]; //OFF_name means 0, OFF_C menas 1, 2 is the question



        server_path(server[index]);



            param += "/cfg/mani_admin_plugin/voterconlist.txt";



            switch(choice)
            {


                    case 1:


                    {

                            ofstream save_del;
                            save_del.open(param.c_str());


                            int rows = voteRconTable->rowCount();


                            for (int i = 0;i<rows;i++)
                            {



                                     save_del << "\"" << voteRconTable->item(i, 0)->text().toStdString() << "\"" << " \"" << voteRconTable->item(i, 1)->text().toStdString() << "\" " << voteRconTable->item(i, 2)->text().toStdString() << endl;




                            }
                            save_del.close();
                    }
                    break;



                    case 2:
                    {
                        unsigned short int counter = 0;

                        ifstream in;
                        in.open(param.c_str());

                        if(in.is_open())
                        {


                                cout << "\n\nCurrent voterconlist: \n";


                                while(!in.eof())
                                {
                                        getline(in, temp);

                                        switch(temp[0])
                                        {
                                                case '"': unsigned int i;
                                                //force new string in vector
                                                buf[OFF_N].push_back("");
                                                buf[OFF_C].push_back("");
                                                buf[2].push_back("");
                                                //  2 means question

                                                for(i=1; i<temp.size(); i++)
                                                {
                                                        if(temp[i]=='"')
                                                                break;

                                                        buf[OFF_N][counter] += temp[i];
                                                }

                                                buf[OFF_N][counter] += '\0'; //0 means end of string

                                                cout << (counter+1) << ".\t" << buf[OFF_N][counter] << endl;

                                                voteRconTable->insertRow(counter);
                                                voteRconTable->setItem(counter, 0, new QTableWidgetItem(buf[OFF_N][counter].c_str()));



                                                i+=3; //to junp over the '"' and over the ' ' and the second '"'

                                                for(; i<temp.size(); i++)
                                                {
                                                        if(temp[i]=='"')
                                                                break;

                                                        buf[2][counter] += temp[i];
                                                }

                                                i+=2; //to junp over the '"' and over the ' '


                                                for(; i<temp.size(); i++)
                                                {
                                                        if((temp[i]=='/' && temp[(i+1)] == '/') || temp[i]==0) //0 means end of string
                                                                break;

                                                        buf[OFF_C][counter] += temp[i];
                                                }


                                                buf[OFF_C][counter] += '\0'; //0 means end of string
                                                buf[2][counter] += '\0'; //0 means end of string
                                                //2 is space for the question
                                                voteRconTable->setItem(counter, 1, new QTableWidgetItem(buf[2][counter].c_str()));
                                                voteRconTable->setItem(counter, 2, new QTableWidgetItem(buf[OFF_C][counter].c_str()));



                                                counter++;

                                                temp.clear(); //clearing to avoid the bug with the letter when temp[] = "\"\" "

                                                break;



                                        }

                                }

                                in.close();

                        }

                        else
                                cout << "\nYou have no rcon questions, please add one!\n";



                    }
                    break;
                    default: cout << "\n\nTry again;)\n";
            }


}


/*bool mani_test(unsigned short int choice)
{
        if (choice > 5)
        {
                cout << "\n\nServer is not supported by mani admin plugin!!\n\n";
                return false;
        }

        if (choice == 3) //gmod
                cout << "\n\nNote that this server isn't officially supported by mani admin plugin\n";

        strcpy(param, workdir);

        switch(choice)
        {
                case 1:


                        strcat(param, "/servers_source/cstrike");


                break;
                case 2:

                        strcat(param, "/servers_source/dod");


                break;
                case 3:

                        strcat(param, "/servers_source/garrysmod");


                break;
                case 4:

                        strcat(param, "/servers_source/garrysmod");


                break;
                case 5:

                        strcat(param, "/servers_source/tf");

                break;
                default: cout << "\n\nTry again ;)\n\n";
                break;
        }

        return true;
}*/



void steamserverutil::sounds(int i, int index, int choice, QTableWidget *soundTable)
{

    //1 means quakesoundlist.txt
    //2 means actionsoundlist.txt
    //3 means soundlist.txt


    server_path(server[index]);


    cout << "From which server do you wish to edit the ";


    if(i == 1)
            cout << "quakesoundlist?\n";
    if(i == 2)
            cout << "actionsoundlist?\n";
    if(i == 3)
            cout << "soundlist?\n";



   if(i == 1)
        param += "/cfg/mani_admin_plugin/quakesoundlist.txt";

    if(i == 2)
        param += "/cfg/mani_admin_plugin/actionsoundlist.txt";

    if(i == 3)
           param += "/cfg/mani_admin_plugin/soundlist.txt";



    vector<string> buf[2]; //OFF_name means 0, OFF_C means 1




            switch(choice)
            {


                    case 1:
                    {

                            ofstream save_del;
                            save_del.open(param.c_str());



                            int rows = soundTable->rowCount();

                            for (int i = 0;i<rows;i++)
                            {


                                   save_del << "\"" << soundTable->item(i, 0)->text().toStdString() << "\"" << " " << soundTable->item(i, 1)->text().toStdString() << endl;


                            }
                            save_del.close();
                    }
                    break;

                    case 2:
                        {
                             unsigned short int counter = 0;

            ifstream in;
            in.open(param.c_str());

            if(in.is_open())
            {


                    cout << "\n\nCurrent list: \n";


                    while(!in.eof())
                    {
                            getline(in, temp);

                            switch(temp[0])
                            {
                                    case '"': unsigned int i;

                                    //force new string in vector
                                    buf[OFF_N].push_back("");
                                    buf[OFF_C].push_back("");

                                    for(i=1; i<temp.size(); i++)
                                    {
                                            if(temp[i]=='"')
                                                    break;

                                            buf[OFF_N][counter] += temp[i];
                                    }

                                    buf[OFF_N][counter] += '\0'; //0 means end of string

                                    cout << (counter+1) << ".\t" << buf[OFF_N][counter] << endl;

                                    soundTable->insertRow(counter);
                                    soundTable->setItem(counter, 0, new QTableWidgetItem(buf[OFF_N][counter].c_str()));



                                    i++; //to junp over the '"' and over the ' '

                                    for(; i<temp.size(); i++)
                                    {
                                            if((temp[i]=='/' && temp[(i+1)] == '/') || temp[i] == 0) //0 means end of string
                                                    break;

                                            buf[OFF_C][counter] += temp[i];
                                    }


                                    buf[OFF_C][counter] += '\0'; //0 means end of string




                                    soundTable->setItem(counter, 1, new QTableWidgetItem(buf[OFF_C][counter].c_str()));

                                    counter++;

                                    temp.clear(); //clearing to avoid the bug with the letter when temp[] = "\"\" "

                                    break;

                                    case 10: //10 means new line
                                    //nothing to do
                                            break;
                                            case 13://13 means return ( '/r' )
                                    //nothing to do
                                                    break;
                                                    case '/': if (temp [1] == '/');
                                                    else
                                                            cout << "\n\nCannot understand " << temp << "\nIgnoring...\n";
                                                    break;
                                                    case 0: break; //nothing to do, 0 means end of string

                                                    default:cout << "\n\nCannot understand " << temp << "\nIgnoring...\n";
                                                    break;

                            }



                    }

                    in.close();

            }

            else
                    cout << "\nYou have no sounds, please add one!\n";



                    }
                    break;
                    default: cout << "\n\nTry again;)\n";
            }


}


void steamserverutil::copy_maps(int index, string folder)
{

        server_path(server[index]);

        param = "cd " + param;

        param += " && cp -v " + folder + "/* maps/ ";

        cout << "\nParam: " <<param<<endl;

        system_check(system(param.c_str()));


}



